import { Injectable } from '@angular/core';
import { ApiService, JsonResult } from './api.service';
import { map } from 'rxjs/operators';
import { HttpClient, HttpParams } from '@angular/common/http';
import { Observable } from 'rxjs/internal/Observable';

import {
  ApplicationListVo,
  PageVo,
  AppCount,
  ApplicationCoefficient,
  ApplicationCVEScore,
  ApplicationGroup,
  ApplicationLicense,
  ApplicationJarseverity,
  ApplicationJarnopermission,
  ApplicationOverview, EchartsVo, AddVo,
  AppProjecstListVo
} from 'src/app/shared/models';

@Injectable({
  providedIn: 'root'
})
export class ApplicationService {


  constructor(
    private apiService: ApiService
  ) { }

  /**
   * 应用下的一些数据
   */
  appCount(projectUuid?: string, appUuid?: string) {
    if (projectUuid && !appUuid) {
      console.log('=== janine./data/project/count/ ===>', projectUuid);
      return this.apiService.get(`/data/project/count/${projectUuid}`)
        .pipe(
          map((jsonResult: JsonResult<AppCount>) => jsonResult)
        );
    } else if (!projectUuid && appUuid) {
      console.log('=== janine./data/application/count =1==>', appUuid);
      const params = new HttpParams()
        .append('appUuid', `${appUuid}`);
      return this.apiService.get('/data/application/count', { params })
        .pipe(
          map((jsonResult: JsonResult<AppCount>) => jsonResult)
        );
    } else {
      console.log('=== janine./data/application/count =2==>', projectUuid);
      return this.apiService.get('/data/application/count')
        .pipe(
          map((jsonResult: JsonResult<AppCount>) => jsonResult)
        );
    }

  }

  /**
  * 获取应用列表
  * @param pageIndex 页码
  * @param pageSize 每页显示几条信息
  */
  getApplicationPage(pageIndex: number, pageSize: number, projectUuid: string): Observable<PageVo<ApplicationListVo>> {
    if (projectUuid) {
      const params = new HttpParams()
        .append('projectUuid', `${projectUuid}`)
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      console.log('=== janine.getApplicationPage().projectUuid =1==>', projectUuid);
      return this.apiService.get(`/data/application/list`, { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<ApplicationListVo>>) => jsonResult.data)
        );
    } else {
      const params = new HttpParams()
        .append('pageIndex', `${pageIndex}`)
        .append('pageSize', `${pageSize}`);
      console.log('=== janine.getApplicationPage().projectUuid =2==>', projectUuid);
      return this.apiService.get(`/data/application/list`, { params })
        .pipe(
          map((jsonResult: JsonResult<PageVo<ApplicationListVo>>) => jsonResult.data)
        );
    }

  }

  // 获取应用列表的总体概况
  getApplicationCoefficient(applicationUuid) {
    return this.apiService.get(`/data/application/coefficient/${applicationUuid}`).pipe(
      map((jsonResult: JsonResult<ApplicationCoefficient>) => jsonResult)
    );
  }

  /**
   * 应用概况
   * @param projectUuid 从项目页传来
   */
  getApplicationOverview() {
    return this.apiService.get('/app/count')
      .pipe(
        map((jsonResult: JsonResult<ApplicationOverview>) => jsonResult)
      );
  }

  getApplicationDetailOverview(projectUuid: string) {
    const params = new HttpParams()
      .append('projectUuid', `${projectUuid}`);
    return this.apiService.get('/application/count', { params })
      .pipe(
        map((jsonResult: JsonResult<EchartsVo>) => jsonResult)
      );
  }


  // 获取组件等级
  getApplicationGrade(applicationUuid) {
    return this.apiService.get(`/data/application/jargrade/${applicationUuid}`).pipe(
      map((jsonResult: JsonResult<ApplicationGroup>) => jsonResult));
  }

  // CVE 得分
  getApplicationCVEScore(applicationUuid) {
    return this.apiService.get(`/data/application/cveseverity/${applicationUuid}`).pipe(
      map((jsonResult: JsonResult<ApplicationCVEScore>) => jsonResult)
    );
  }


  // 许可使用
  getApplicationLicense(applicationUuid) {
    return this.apiService.get(`/data/application/licenseisopen/${applicationUuid}`).pipe(
      map((jsonResult: JsonResult<ApplicationLicense>) => jsonResult)
    );
  }

  // 获取高危组件
  getApplicationJarseverity(applicationUuid) {
    return this.apiService.get(`/data/application/jarseverity/${applicationUuid}`)
      .pipe(
        map((jsonResult: JsonResult<ApplicationJarseverity>) => jsonResult)
      );
  }


  // 获取未授权
  getApplicationJarnopermission(applicationUuid) {
    return this.apiService.get(`/data/application/jarnopermission/${applicationUuid}`)
      .pipe(
        map((jsonResult: JsonResult<ApplicationJarnopermission>) => jsonResult)
      );
  }

  fileUpload(url: string, info: any) {
    return this.apiService.post(url, info).pipe(
      map((jsonResult: JsonResult<AddVo>) => jsonResult.data)
    );
  }

  downloadFile() {
    return this.apiService.get('/data/download/dependencyjson', { responseType: 'blob' });
  }


  getAppProjectList(groupUuid?: string) {
    if (groupUuid) {
      const params = new HttpParams()
        .append('groupUuid', `${groupUuid}`);
      return this.apiService.get('/data/application/projectlist', { params })
        .pipe(
          map((jsonResult: JsonResult<AppProjecstListVo>) => jsonResult)
        );
    } else {
      return this.apiService.get('/data/application/projectlist')
        .pipe(
          map((jsonResult: JsonResult<AppProjecstListVo>) => jsonResult)
        );
    }

  }

  /**
   * 删除应用
   * @param info 传过来的信息
   */
  deleteApp(appUuid: string) {
    const params = new HttpParams()
      .append('appUuid', `${appUuid}`);
    return this.apiService.get('/data/application/delete', { params })
      .pipe(
        map((jsonResult: JsonResult<AddVo>) => jsonResult)
      );
  }
}
